{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bdf03658",
   "metadata": {},
   "source": [
    "# sorted 排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "056b930c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1, 3, 4, 5, 9]\n"
     ]
    }
   ],
   "source": [
    "nums = [3, 1, 4, 1, 5, 9]\n",
    "sorted_nums = sorted(nums)  # 升序（默认）\n",
    "print(sorted_nums)  # 输出：[1, 1, 3, 4, 5, 9]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5cca185f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'banana', 'cherry']\n"
     ]
    }
   ],
   "source": [
    "#  对字符串排序（按 ASCII 码）\n",
    "words = [\"banana\", \"apple\", \"cherry\"]\n",
    "sorted_words = sorted(words)\n",
    "print(sorted_words)  # 输出：['apple', 'banana', 'cherry']（按首字母顺序）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8c63b507",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['date', 'apple', 'banana', 'cherry']\n"
     ]
    }
   ],
   "source": [
    "# 按字符串长度排序\n",
    "words = [\"apple\", \"banana\", \"cherry\", \"date\"]\n",
    "# 按字符串长度升序（短→长）\n",
    "sorted_by_length = sorted(words, key=lambda x: len(x))\n",
    "print(sorted_by_length)  # 输出：['date', 'apple', 'banana', 'cherry']\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "langchain-course (3.10.12)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
